package com.enterprise.application.mvc.struts.view.freemarker;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.ServletContext;

import com.enterprise.support.utility.Enterprise;

import freemarker.ext.beans.BeansWrapper;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import freemarker.template.TemplateHashModel;

/**
 * Created by IntelliJ IDEA.
 * User: Zhanggaojiang
 * Date: 11-1-19
 * Time: 上午10:07
 * Email: z82422@gmail.com
 * Freemarker 扩展,
 * 自动加载 config/freemarker/autoimport.properties，config/freemarker/template.properties
 */
public class FreemarkerManager extends org.apache.struts2.views.freemarker.FreemarkerManager {
    @Override
    public void init(ServletContext servletContext) throws TemplateException {
        super.init(servletContext);
        Configuration config = this.getConfiguration(servletContext);
        config.setTemplateExceptionHandler(new TemplateExceptionHandler());
        config.setNumberFormat("#");
        try {

            //number_format=#

            //Auto Import
            Enterprise<String, Object> templates = new Enterprise(com.enterprise.support.utility.Configuration.getProperties("/config/freemarker/autoimport.properties"));
            config.setAutoImports(templates);

            //设置 在freemarker内可以调用到的 函数
            config.setSharedVariable("statics", BeansWrapper.getDefaultInstance().getStaticModels());
            templates = new Enterprise(com.enterprise.support.utility.Configuration.getProperties("/config/freemarker/template.properties"));
            Map<String, Object> values = new HashMap();
            for (String key : templates.keySet()) {
                getTemplates(values, 0, key.split("\\."), templates.getString(key));
            }
            for (String key : values.keySet()) {
                config.setSharedVariable(key, values.get(key));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    /**
     * 递归形成 树状结构
     */
    public static Map getTemplates(Map values, int index, String[] keys, String value) {
        if (values == null) values = new HashMap();
        if (index < keys.length - 1) {
            if (!values.containsKey(keys[index])) {
                values.put(keys[index], new HashMap());
            }
            getTemplates((Map) values.get(keys[index]), index + 1, keys, value);
        } else {
            try {
                values.put(keys[index], Class.forName(value).newInstance());
            } catch (Exception ex) {
                TemplateHashModel staticModels = BeansWrapper.getDefaultInstance().getStaticModels();
                try {
                    values.put(keys[index], staticModels.get(value));
                } catch (Exception ex2) {
                    ex2.printStackTrace(System.err);
                }
            }
        }
        return values;
    }

}
